package net.anzhuo.controller.db.syncdata;



import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import lombok.extern.slf4j.Slf4j;

import net.anzhuo.entity.GsPunishment;
import net.anzhuo.entity.db.syncdata.PageUtilsPermission;
import net.anzhuo.entity.db.syncdata.PageUtilsPunishment;
import net.anzhuo.entity.db.syncdata.Paging;
import net.anzhuo.entity.db.syncdata.Penalty;
import net.anzhuo.entity.db.syncdata.QueryPunishmentResult;
import net.anzhuo.entity.db.syncdata.Result;
import net.anzhuo.service.GsBaseService;

import net.anzhuo.service.GsPunishmentService;

@RestController
@Slf4j
public class PunishmentController {

	@Autowired
	private GsBaseService gsBaseService;

	@Autowired
	private GsPunishmentService gsPunishmentService;
	
	
	
	@RequestMapping("/querypunishment")
	public QueryPunishmentResult querypunishment(String keyNo,String date,String pageIndex, Integer pageSize) {
		log.info("===>querypunishment="+keyNo);
		long startTime=System.currentTimeMillis();
		/*try {
			DetailResult dr = (DetailResult)RedisUtil.get("detail:"+keyNo);
			if (dr != null) {
				 long endTime=System.currentTimeMillis();
				 float excTime=(float)(endTime-startTime)/1000;
				 log.info(" getDetail_keyword 执行时间："+excTime+"s");
				log.info("===>getDetail_keyword= redis is hitted!");
				return dr;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}*/
		QueryPunishmentResult detailResult = new QueryPunishmentResult();
		try {
	
		detailResult.setStatus("200");
		detailResult.setMessage("查询成功");
		GsPunishment  p=new GsPunishment();
		p.setCoMd5(keyNo);
		p.setUpdateTime(date);
		List<GsPunishment> listp=gsPunishmentService.getByUpdateTime(p);
		Paging paging = new Paging();
		paging.setPageIndex(Integer.parseInt(pageIndex));
		paging.setPageSize(pageSize);
		
		paging.setTotalRecords(listp.size());
		
		
		detailResult.setPaging(paging);
		List<Penalty> listPenalty=new ArrayList<Penalty>();
		if(listp!=null)
		{
			Penalty pl=new Penalty();
			for(GsPunishment g:listp)
			{
				pl=new Penalty();
				pl.setDocNo(g.getDocNo());
				pl.setContent(g.getContent());
				pl.setOfficeName(g.getOfficeName());
				pl.setPenaltyDate(g.getPenaltyDate());
				pl.setPublicDate(g.getPublicDate());
				pl.setPenaltyType(g.getPenaltyType());
				listPenalty.add(pl);
			}
		}
		
        PageUtilsPunishment pup=new PageUtilsPunishment(listPenalty,paging.getPageIndex().intValue(),paging.getPageSize().intValue());
		
		detailResult.setPenalty(pup.result);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	
		
		 long endTime=System.currentTimeMillis();
		 float excTime=(float)(endTime-startTime)/1000;
		 log.info(" querypunishment 执行时间："+excTime+"s");
		return detailResult;
	}
	
	
	@RequestMapping("/updatepunishment")
	public Result updatepunishment(String punishmentNo,String legal,String type,String result,String reason,String lawItems,String punishmentAgency,String decisionDate,String updateTime,String keyNo,String punishDate) {
		log.info("===>updatepunishment="+keyNo);
		long startTime=System.currentTimeMillis();
		/*try {
			DetailResult dr = (DetailResult)RedisUtil.get("detail:"+keyNo);
			if (dr != null) {
				 long endTime=System.currentTimeMillis();
				 float excTime=(float)(endTime-startTime)/1000;
				 log.info(" getDetail_keyword 执行时间："+excTime+"s");
				log.info("===>getDetail_keyword= redis is hitted!");
				return dr;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}*/
		Result detailResult = new Result();
		try {
	
		detailResult.setStatus("200");
		detailResult.setMessage("插入数据成功");
		GsPunishment  p=new GsPunishment();
		p.setCoMd5(keyNo);
		p.setDocNo(punishmentNo);
		p.setContent(result);
		p.setOfficeName(punishmentAgency);
		p.setPublicDate(decisionDate);
		p.setPenaltyDate(punishDate);
		p.setRemark(reason);
		p.setPenaltyType(type);
		p.setUpdateTime(updateTime);
		List<GsPunishment> listp=gsPunishmentService.getBycoMd5(keyNo);
		boolean b=false;
		if(listp!=null)
		{
			for(GsPunishment g:listp)
			{
				if(g.getDocNo().equals(p.getDocNo())&&g.getContent().equals(p.getContent())&&g.getPenaltyDate().equals(p.getPenaltyDate()))
				{
					b=true;
					detailResult.setStatus("201");
					detailResult.setMessage("数据已存在");
					break;
				}
			}
		}
		if(!b)
		{
			gsPunishmentService.save(p);
		}		
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	
		
		 long endTime=System.currentTimeMillis();
		 float excTime=(float)(endTime-startTime)/1000;
		 log.info(" updatepunishment 执行时间："+excTime+"s");
		return detailResult;
	}
	
	
}
